package com.stlm2.dems.informs.service;

import com.stlm2.core.common.page.PageView;
import com.stlm2.dems.informs.entity.InformsState;
import com.stlm2.dems.dao.UserDao;
import com.stlm2.dems.entity.User;
import com.stlm2.util.codecontent.CodeContent;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;

import com.stlm2.dems.informs.entity.Informs;
import com.stlm2.dems.informs.dao.InformsDao;
import com.stlm2.core.base.BaseDao;
import com.stlm2.core.base.impl.BaseServiceImpl;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("informsService")
@Transactional(rollbackForClassName={"java.lang.Exception"}, propagation = Propagation.REQUIRED)
public class InformsService extends BaseServiceImpl<Informs> {

	@Resource
	private InformsDao informsDao;
	@Resource
	private UserDao userDao;

	@Override
	protected BaseDao <Informs> getDao() {
		return informsDao;
	}
	/*
	*ZLQ
	*查询新闻列表
	 */
	public String getInformsList(ModelMap modelMap ,String page,InformsState informsState){
		PageView pageview=new PageView();
		int pageNo = 1;
		int pageSize=20;
		List<ModelMap> list=new ArrayList<ModelMap>();
		if (NumberUtils.isDigits(page) && !"0".equals(page.trim())) {
			pageNo = NumberUtils.toInt(page);
		}
		Informs informs =new Informs();
		if(informsState !=null){
			informs.setState(informsState);
		}
		pageview = informsDao.queryPageView(pageNo,pageSize,informs);
		if(pageview!=null){
			list=convert_v1(pageview.getRecords());
		}
		return  CodeContent.getModelByCode(1,"", list);
	}
	public List<ModelMap> convert_v1(List<Informs> informss) {
		List<ModelMap> list = new ArrayList<ModelMap>();
		if (informss != null && informss.size() > 0) {
			for (Informs informs : informss) {
				ModelMap map = buildModel_v1(informs);
				list.add(map);
			}
		}

		return list;
	}
	public ModelMap buildModel_v1(Informs informs) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		ModelMap model = new ModelMap();
		model.put("Id",informs.getId());
		model.put("Title",informs.getTitle());
		model.put("CreateTime",formatter.format(informs.getCreateTime()));
		model.put("imgUrl",informs.getImgUrl()!=null?informs.getImgUrl():"");
		return model;
	}
	/*
	*ZLQ
	* 根据ID查询新闻详情
	* Id：新闻ID
	 */
	public String getDetails(ModelMap model,String Id){
		if (StringUtils.isEmpty(Id)) {
			return CodeContent.getModelByCode(0, "ID不能为空", model);
		}
		Informs informs=informsDao.getById(Integer.parseInt(Id));
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		if(informs!=null){
			model.put("Id",informs.getId());
			model.put("Title",informs.getTitle());
			model.put("Content",informs.getContent());
			formatter.format(informs.getCreateTime());
			model.put("CreateTime",formatter.format(informs.getCreateTime()));
			model.put("imgUrl",informs.getImgUrl()!=null?informs.getImgUrl():"");
		}
		return 	 CodeContent.getModelByCode(1, "", model);
	}

		/*
		*ZLQ
	*添加新闻信息
	*title：标题
	* content：内容
	* token：用户token
	 */
	public String addInforms(ModelMap model,String title,String content ,String token, String urlImg){
		if (StringUtils.isEmpty(token)) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		User loginUser = userDao.getByToken(token);
		if (loginUser == null) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		if (StringUtils.isEmpty(title)) {
			return CodeContent.getModelByCode(0, "标题不能为空", model);
		}
		if (StringUtils.isEmpty(content)) {
			return CodeContent.getModelByCode(0, "内容不能为空", model);
		}
		if (StringUtils.isEmpty(urlImg)) {
			return CodeContent.getModelByCode(0, "图片路径不能为空", model);
		}
		Informs informs=new Informs();
		informs.setTitle(title);
		informs.setContent(content);
		informs.setState(InformsState.Valid);
		informs.setCreateTime(new Date());
		informs.setCreateUserId(loginUser.getId());
		informs.setImgUrl(urlImg);
		informsDao.add(informs);
		return CodeContent.getModelByCode(1, "", model);
	}
		/*
		*ZLQ
	*修改新闻信息
	* title：标题
	* content：内容
	* token：用户token
	*
	 */
	public String updateInforms(ModelMap model,String Id,String title,String content ,String token,String stateValue,String urlImg){
		if (StringUtils.isEmpty(token)) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		User loginUser = userDao.getByToken(token);
		if (loginUser == null) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		if (StringUtils.isEmpty(Id)) {
			return CodeContent.getModelByCode(0, "ID不能为空", model);
		}
		if (StringUtils.isEmpty(title)) {
			return CodeContent.getModelByCode(0, "标题不能为空", model);
		}
		if (StringUtils.isEmpty(content)) {
			return CodeContent.getModelByCode(0, "内容不能为空", model);
		}
		if (StringUtils.isEmpty(urlImg)) {
			return CodeContent.getModelByCode(0, "图片路径不能为空", model);
		}

		Informs informs=informsDao.getById(Integer.parseInt(Id));
		if(informs==null){
			return CodeContent.getModelByCode(0, "ID不存在", model);
		}
		if(stateValue.equals("0")){
			informs.setState(InformsState.Invalid);
		}
		informs.setTitle(title);
		informs.setContent(content);
		informs.setImgUrl(urlImg);
		informsDao.modify(informs);
		return CodeContent.getModelByCode(1, "", model);
	}
   //获取六条最新的新闻
	public String getSixSortByTime(){
		List<Informs> informs=informsDao.getSixSortByTime();
		return CodeContent.getModelByCode(1, "",convertSimple(informs) );
	}

	public List<ModelMap> convertSimple(List<Informs> informss) {
		List<ModelMap> list = new ArrayList<ModelMap>();
		if (informss != null && informss.size() > 0) {
			for (Informs informs : informss) {
				ModelMap map = buildModelSimple(informs);
				list.add(map);
			}
		}
		return list;
	}
	// 标题 ，id
	public ModelMap buildModelSimple(Informs informs) {
		ModelMap model = new ModelMap();
		model.put("id",informs.getId());
		model.put("title",informs.getTitle());
		model.put("imgUrl",informs.getImgUrl()!=null?informs.getImgUrl():"");
		return model;
	}
}